﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FreedomERP.Services.Bd.Impl;
using FreedomERP.Models;
using System.Collections;
using FreedomERP.Comm;
using System.Text;
using System.Data.Objects;

namespace FreedomERP.Dao.Bd.Impl
{
    public class BdAccountDaoImpl : IBdAccountDao
    {
        FreedomERPEntities dbEntities = new FreedomERPEntities();

        /*
         * 根据条件获取帐号列表
         */
        public List<BdAccount> QueryAccountPaggingByParam(PagingParams paggingParams, out int totalCount)
        {
            Hashtable filters = paggingParams.filters;

            //where
            List<ObjectParameter> paramList = new List<ObjectParameter>();
            StringBuilder sbWhere = new StringBuilder();
            if (filters["accountParam"] != null && !filters["accountParam"].ToString().Equals(""))
            {
                sbWhere.Append("it.FAccount like @accountParam and ");
                ObjectParameter param = new ObjectParameter("accountParam", "%" + filters["accountParam"].ToString() + "%");
                paramList.Add(param);
            }
            if (filters["roleParam"] != null && !filters["roleParam"].ToString().Equals(""))
            {
                sbWhere.Append("it.FRoleId=@roleParam and ");
                ObjectParameter param = new ObjectParameter("roleParam", filters["roleParam"].ToString());
                paramList.Add(param);
            }
            if (filters["statusParam"] != null && !filters["statusParam"].ToString().Equals(""))
            {
                sbWhere.Append("it.FStatusId=@statusParam and ");
                ObjectParameter param = new ObjectParameter("statusParam", filters["statusParam"].ToString());
                paramList.Add(param);
            }
            else
            {

                IBdCategoryService cateService = new BdCategoryServiceImpl();
                BdCategory acctCate = cateService.GetCategoryByFNumberAndFType(ConstData.ACCOUNT_STATUS_NO, ConstData.CATEGORY_ACCOUNT_STATUS);
                if (acctCate != null)
                {
                    sbWhere.Append("it.FStatusId!=@statusParam and ");
                    ObjectParameter param = new ObjectParameter("statusParam", acctCate.FId);
                    paramList.Add(param);
                }

            }

            sbWhere.Append("it.FOrgunitId=@orgunitParam and ");
            ObjectParameter paramOrg = new ObjectParameter("orgunitParam", filters["orgunitParam"].ToString());
            paramList.Add(paramOrg);

            sbWhere.Append("1=1");

            //order by
            StringBuilder sbOrder = new StringBuilder();
            sbOrder.Append("it." + paggingParams.Sort + " " + paggingParams.Dir);

            ObjectParameter[] objParameters = new ObjectParameter[paramList.Count];
            int c = 0;
            foreach (ObjectParameter paramTemp in paramList)
            {
                objParameters[c] = paramTemp;
                c++;
            }

            var query = dbEntities.BdAccount.Where(sbWhere.ToString(), objParameters).OrderBy(sbOrder.ToString());
            totalCount = query.Count();

            List<BdAccount> accountList = query.Skip(paggingParams.Start).Take(paggingParams.Limit).ToList();

            return accountList;
        }

        /*
         * 根据帐号获取信息
         */
        public BdAccount GetAccountByFAccount(String account)
        {
            BdAccount bdAccount = null;

            List<BdAccount> accountList = (from ba in dbEntities.BdAccount where ba.FAccount.Equals(account) select ba).ToList<BdAccount>();
            if (accountList != null && accountList.Count > 0)
            {
                bdAccount = accountList.First();
                if (bdAccount.BdCategory.FNumber == ConstData.ACCOUNT_STATUS_NO)
                    bdAccount = null;
            }

            return bdAccount;
        }

        /*
         * 根据FId获取信息
         */
        public BdAccount GetAccountByFId(String FId)
        {
            BdAccount bdAccount = null;

            List<BdAccount> accountList = (from ba in dbEntities.BdAccount where ba.FId.Equals(FId) select ba).ToList<BdAccount>();
            if (accountList != null && accountList.Count > 0)
            {
                bdAccount = accountList.First();
                if (bdAccount.BdCategory.FNumber == ConstData.ACCOUNT_STATUS_NO)
                    bdAccount = null;
            }

            return bdAccount;
        }

        /*
         * 添加帐号
         */
        public void AddAccount(BdAccount account)
        {
            dbEntities.BdAccount.AddObject(account);
        }

        /*
         * 删除帐号
         */
        public void DeleteAccount(BdAccount account)
        {
            IBdCategoryService cateService = new BdCategoryServiceImpl();
            BdCategory acctCate = cateService.GetCategoryByFNumberAndFType(ConstData.ACCOUNT_STATUS_NO, ConstData.CATEGORY_ACCOUNT_STATUS);
            if (acctCate == null)
            {
                throw new Exception("账户状态信息不存在，删除账户失败。");
            }
            if (account.FStatusId == acctCate.FId)
            {
                throw new Exception("该账户已注销。");
            }
            account.FStatusId = acctCate.FId;
        }

        /*
        * 保存修改
        */
        public void Save()
        {
            dbEntities.SaveChanges();
        }
    }
}
